#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//#include <time.h>
//#include <sys/time.h>
#include <mysql/mysql.h>
#include "fileUtil.h"
//#include "config.h"
//#include "configField.h"

#include <pthread.h>
#include <stdbool.h>
#include "linkQueue.h"

static MYSQL *mysqlClient = NULL;


void mysql() {
    int ret = mysql_library_init(0, NULL, NULL);
    printf("enter init mysql func...\n");
    mysqlClient = mysql_init(NULL);

    if (mysql_real_connect(mysqlClient, "127.0.0.1", "root", "88stIVA#2019", "cdemo", 10208, NULL, 0) == NULL) {
        printf("failed connect to mysql,%s\n", mysql_error(mysqlClient));
        return;
    }
    char *updateSql = "update t1 set version=66680;";
    mysql_query(mysqlClient, updateSql);
    int rows = mysql_affected_rows(mysqlClient);
    printf("update %d rows\n", rows);
    mysql_close(mysqlClient);
    mysql_library_end();


}


void mysql2() {
    printf("init mysql connection\n");
    const char *clientInfo = mysql_get_client_info();
    printf("mysql client: %s\n", clientInfo);
    mysqlClient = mysql_init(NULL);
#ifdef DEBUG
    printf("mysql client ptr: %p\n", mysqlClient );
#endif

    MYSQL *mysqlConnectResult = mysql_real_connect(mysqlClient, "127.0.0.1", "root", "88stIVA#2019", "cdemo", 10208,
                                                   NULL, 0);
//    MYSQL *mysqlConnectResult = mysql_real_connect(mysqlClient, mysqlHost, mysqlUser, mysqlPassword, mysqlDB, mysqlPort, NULL, 0);
    if (mysqlConnectResult == NULL) {
        printf("failed connect to mysql,%s\n", mysql_error(mysqlClient));
        exit(1);
    }
    printf("connect to mysql success\n");
    mysql_close(mysqlClient);
    mysql_library_end();


}


void *splitStringtest() {
    char str[200] = "qwew,erer,tryry";
    char token = ',';
    char *start = str;
#define TRUE 1
    char xxx = TRUE;

    char *after;
    while ((after = strchr(start, token)) != NULL) {
        *after = 0;
        char *split = calloc(10, sizeof(char));
        strcpy(split, start);
        printf("%s\n", split);
        start = after + 1;
    }
    printf("%s\n", start);

}

pthread_mutex_t lock;


void *dos(void *n) {
    pthread_mutex_lock(&lock);
    printf("=========%d============\n", (int) n);
    pthread_mutex_unlock(&lock);
    pthread_exit(n);
}

void testMultiThread() {
    pthread_mutex_init(&lock, NULL);
//    int i=10;

    for (int i = 0; i < 5; ++i) {
        pthread_t tid;
        int j = i;
        pthread_create(&tid, NULL, dos, (void *) j);
    }
}








LinkQueue *queue;
int total = 1000000;


void *produce(void *arg){
    for (int i = 0; i < total; ++i) {
            char *str = calloc(10, sizeof(char));
            strcpy(str, "abc===");
            memset(str + strlen(str), 'A' + i%50, 1);
            enQueue(queue, str);
    }

}

void *consume(void *arg){

    while(1){
        int jj=0;
        char *data = deQueue(queue);
        if (data != NULL) {
            printf("get [%s] from queue\n", data);
            free(data);
        }
        jj++;
        if(jj>=total){
            printf("end=============\n");
            return NULL;
        }


    }

}

void testQueue(){

    queue = newQueue(200);
    char *sss = "aaabbb===";
    for (int i = 0; i < 300; ++i) {
        char *str = calloc(10, sizeof(char));
        strcpy(str, sss);
        memset(str + 9, 'A' + i%50, 1);
        enQueue(queue, str);
    }

    for (int j = 0; j < 205; ++j) {
        char *data = deQueue(queue);
        if (data != NULL) {
            printf("get [%s] from queue\n", data);
            free(data);
        }
    }
}

void pandc(){

    queue = newQueue(1000);
    pthread_t tp;
    pthread_t tc;
    pthread_create(&tp,NULL,produce,NULL);
    pthread_create(&tc,NULL,consume,NULL);
    pthread_join(tp,NULL);
    pthread_join(tc,NULL);


}

int main() {
//    pandc();

    char *fmt = "%s-%s-%s-%s";
    char str[100]={0};
    char str2[100]={0};
    char str3[100]={0};
    sprintf(str,fmt,"abc");
    fprintf(stdout,"%s==\n",str);
//    sprintf(str2,str,"abc");
//    fprintf(stdout,"%s==\n",str2);
//    sprintf(str3,str2,"abc");
//    fprintf(stdout,"%s==\n",str3);

    // ( values ),(



}

void xxx() {

    /**

     pthread_create
     pthread_exit
     pthread_join

     //怎么获取线程返回 pthread_exit(returnvalue) ;  主线程 pthread_join(&tid,ptr); ptr 用于接收这个返回值

     */

    pthread_cond_t condition;
    pthread_cond_init(&condition, NULL);
    pthread_cond_wait(&condition, &lock);
    pthread_cond_signal(&condition);

    pthread_cond_destroy(&condition);

    pthread_mutex_init(&lock, NULL);
    pthread_mutex_lock(&lock);
    pthread_mutex_unlock(&lock);


    pthread_mutex_destroy(&lock);

}


